﻿
using UnityEngine;
using System.Collections;
/*
[RequireComponent(typeof(UISlider))]
public class TweenEnergyPower : MonoBehaviour {
	UISlider _power;
	float _targetValue;
	float _targetOutValue;
	float _nowValue = 0;
	public float _addSpeed = 1;
	// Use this for initialization
	void Start () {
		_power = GetComponent<UISlider> ();
		InitState ();
	}
	
	// Update is called once per frame
	void Update () {
		_state.UpdateState (Time.deltaTime);

	}

	#region State
	FSM _state;
	StatePath _idle = new StatePath("Idle");
	StatePath _add = new StatePath("Add");
	StatePath _sub = new StatePath("Sub");

	void InitState(){
		_state = new FSM ();
		_state.AddState (_out, OnIdleEnter);
		_state.AddState (_back, OnAddEnter, OnAddUpdate);
		_state.AddState (_sleep, OnSubEnter, OnSubUpdate);

		_state.SetState (_idle);
	}
	#region Idle
	void OnIdleEnter(){
		
	}
	#endregion
	#region Add
	void OnAddEnter(){
		
	}
	void OnAddUpdate(float dt){	
		_nowValue += _addSpeed * dt;
		UpdateValue ();
	}
	void OnAddLeave(){}
	#endregion
	#region Sub
	void OnSubEnter(){}
	void OnSubUpdate(float dt){
		
	}
	#endregion 

	#endregion
	void UpdateValue(){
		_power.value = _nowValue;
	}

	#region Interface
	public void SetValue(float value){
		_targetValue = value;
		_state.SetState (_add);
	}
	#endregion
	#region Test
	void OnGUI(){
		for (float i = 0; i < 1; i += 0.1f) {
			if(GUILayout.Button(i.ToString()))
				SetValue(i);
		}
	}
	#endregion
}
*/
[RequireComponent(typeof(UISlider))]
public class TweenEnergyPower : MonoBehaviour {


	UISlider _power;
	float _nowvalue = 0;
	float _targetValue;
	float _targetOutValue;
    float _addSpeed = 0.3f;
	bool _dir;
    float reSpeed;

	void Awake(){

		_power = GetComponent<UISlider> ();

		InitState ();

	}

	#region state

	FSM _state;
	StatePath _out = new StatePath("out");
	//StatePath _back = new StatePath("back");
	StatePath _sleep = new StatePath("sleep");

	void InitState(){

		_state = new FSM ();
		_state.AddState (_out, OnOutEnter, OnOutUpdate);
		//_state.AddState (_back, OnBackEnter, OnBackUpdate);
		_state.AddState (_sleep, OnSleepEnter);

	}

	#region out

	void OnOutEnter(){

		//Debug.Log ("ZENGJIA");
		//_dir = _nowvalue < _targetOutValue;
		UpdateValue ();
	}

	void OnOutUpdate(float dt){

        _nowvalue = Mathf.SmoothDamp(_nowvalue, _targetValue,ref reSpeed,_addSpeed);
		//_nowvalue += _addSpeed * dt * (_dir == true ? 1 : -1);
		UpdateValue ();
		//if ((_dir && _nowvalue >= _targetOutValue) || (!_dir && _nowvalue <= _targetOutValue)) {
		//	_nowvalue = _targetOutValue;
		//	//_state.SetState (_back);
		//}

	}

	#endregion

	#region back

//	void OnBackEnter(){
////		this.DebugLog ("JIANSHAO", DebugType.UI);
//		_dir = _nowvalue < _targetValue;
//		UpdateValue ();

//	}

//	void OnBackUpdate(float dt){
//		_nowvalue += _addSpeed * dt * (_dir == true ? 1 : -1);
//		UpdateValue ();
//		if ((_dir && _nowvalue >= _targetValue) || (!_dir && _nowvalue <= _targetValue)) {
//			_nowvalue = _targetValue;
//			UpdateValue ();
//			_state.SetState (_sleep);
//		}

//	}
	#endregion
	#region sleep

	void OnSleepEnter(){

		//Debug.Log ("开始休眠");
	}

	#endregion

	#endregion

	#region setvalue

	public void SetValue(float value){

		_targetValue = value;
		//_targetOutValue = _targetValue + (Mathf.Abs(_targetValue - _nowvalue)*0.1f) * (_targetValue - _nowvalue > 0 ? 1 : -1);
		_state.SetState (_out);
	}

	#endregion

	void UpdateValue(){

		_power.value = _nowvalue;
	}


	void Update () {
		_state.UpdateState (Time.deltaTime);
	}

}

